import http from "../../../utils/https";

// mine/pages/myContractDetail/myContractDetail.js
Page({
  data: {
    contractId: '',
    contractDetail: {},
    residentInfo:{},
    loading: true,
    error: false
  },

  onLoad: function (options) {
    const contractId = options.contractId;
    if (contractId) {
      this.setData({
        contractId: contractId
      });
      this.loadContractDetail();
    } else {
      this.setData({
        error: true,
        loading: false
      });
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
    }

    this.getResidentInfo()

  },

  // 加载合同详情
  loadContractDetail: function () {
    const app = getApp();
    const baseUrl = app.globalData.baseUrl;
    const token = wx.getStorageSync('token');
    const contractId = this.data.contractId;

    if (!token) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      return;
    }

    this.setData({
      loading: true,
      error: false
    });


    // 模拟网络延迟
    setTimeout(() => {
      this.setData({
        loading: false
      });
    }, 1000);
    http.get(`contract/contract/byId/${contractId}`)
    .then(resp=>{
      this.setData({
        contractDetail:resp.data
      })
    })
    .catch()
  },

  //加载住户详情
  getResidentInfo(){
    const residentId=wx.getStorageSync('residentId')
    http.get(`resident/resident/${residentId}`)
    .then(resp=>{
      this.setData({
        residentInfo:resp.data
      })
    })
    .catch(err=>{
      console.log("请求失败",err);
    })
  },


  // 联系房东
  contactLandlord: function () {
    wx.showActionSheet({
      itemList: ['拨打电话', '发送短信', '微信联系'],
      success: (res) => {
        switch (res.tapIndex) {
          case 0:
            wx.makePhoneCall({
              phoneNumber: '13800138000',
              fail: (err) => {
                wx.showToast({
                  title: '拨打失败',
                  icon: 'none'
                });
              }
            });
            break;
          case 1:
            wx.showToast({
              title: '短信功能开发中',
              icon: 'none'
            });
            break;
          case 2:
            wx.showToast({
              title: '微信联系功能开发中',
              icon: 'none'
            });
            break;
        }
      }
    });
  },

  // 导航到续约页面
  navigateToRenew: function () {
    wx.navigateTo({
      url: '/mine/pages/renew/renew?contractId=' + this.data.contractId
    });
  },

  // 导航到终止合同页面
  navigateToTerminate: function () {
    wx.showModal({
      title: '申请终止合同',
      content: '确定要申请终止此合同吗？此操作不可撤销。',
      confirmColor: '#ff4d4f',
      success: (res) => {
        if (res.confirm) {
          // 获取终止原因
          wx.showActionSheet({
            itemList: ['个人原因', '房屋问题', '工作变动', '其他原因'],
            success: (result) => {
              const reasons = ['个人原因', '房屋问题', '工作变动', '其他原因'];
              const selectedReason = reasons[result.tapIndex];
              
              // 导航到终止合同结果页面
              wx.navigateTo({
                url: `/mine/pages/terminate/terminate?contractId=${this.data.contractId}&reason=${selectedReason}`
              });
            }
          });
        }
      }
    });
  },

  // 重试加载
  retryLoad: function () {
    this.loadContractDetail();
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.loadContractDetail();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});